<?php

/**
 * 日志专用类  公用
 * @author ly-chengminbin
 *
 */

namespace application\depend;

/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @return mixed
 */
function getClientIp($type = 0)
{
    $type = $type ? 1 : 0;
    static $ip = NULL;
    if ($ip !== NULL) return $ip[$type];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos) unset($arr[$pos]);
        $ip = trim($arr[0]);
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}//end get ip


class Applog
{

    private static $logsInfo = "xingai-logs";

    public static function errorLog($data, $file = "", $line = "",$logsInfo = "")
    {
        $rootPath = APP_LOG_PATH_NEW . '/error';
        try {
            if (!is_dir($rootPath)) {
                @mkdir($rootPath, 0777, true);
            }
            if (is_array($data)) {
                $data['file'] = $file;
                $data['line'] = $line;
                $data['user_ip'] = getClientIp();
                $data['log_time'] = date("Y-m-d H:i:s");
                $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            }
            $file = '/'.$rootPath . date("Y-m-d");
            $res = fopen($file, 'a');
            fwrite($res, $data . PHP_EOL);
            fclose($res);
        } catch (\Exception $e) {

        }

    }

    /**
     * 在控制行中输出
     */
    public static function writeOutConsole($msg = "")
    {
        is_array($msg) && $msg = json_encode($msg,JSON_UNESCAPED_UNICODE);
        fwrite(STDOUT, date("Y-m-d H:i:s") . ':' . $msg . PHP_EOL);
    }


    /**
     * @param array $data
     * @param string $file
     * @param int $line
     * 调试日志
     */
    static public function debugLogs($data = [], $file = __FILE__, $line = __LINE__, $logsInfo = "")
    {
        if(APP_DEBUG_LOG) {
            $rootPath = APP_LOG_PATH_NEW;
            try {
                if (!is_dir($rootPath)) {
                    @mkdir($rootPath, 0777, true);
                }
                empty($logsInfo) && $logsInfo = self::$logsInfo;
                if (is_array($data)) {
                    $data['file'] = $file;   //普通日志
                    $data['line'] = $line;
                    $data['user_ip'] = getClientIp();
                    $data['log_time'] = date("Y-m-d H:i:s");
                    $data = json_encode($data, JSON_UNESCAPED_UNICODE);
                } else {
                    $data = date("Y-m-d H:i:s") . " " . $data . ":" . $file . '->' . $line;
                }
                $file = $rootPath . '/debug_' . $logsInfo . '_' . date("Y-m-d");
                $res = fopen($file, 'a');
                fwrite($res, $data . PHP_EOL);
                fclose($res);
                //  APP_DEBUG_LOG && self::writeOutConsole($data);
            } catch (\Exception $e) {

            }
        }
    }//end

    static public function infoLog($data, $errfile = __FILE__, $line = __LINE__)
    {
        if(APP_INFO_LOG) {
            $rootPath = APP_LOG_PATH_NEW . '/info';
            if (!is_dir($rootPath)) {
                @mkdir($rootPath, 0777, true);
            }
            $file = $rootPath . '/' . date("Y-m-d");
            $res = fopen($file, 'a');
            if (is_array($data)) {
                $data['file'] = $errfile;
                $data['line'] = $line;
                $data['log_time'] = date("Y-m-d H:i:s");
                $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            }
            fwrite($res, $data . PHP_EOL);
            fclose($res);
        }//end if
    }//end function
    //
    static public function fileLog($dir, $data, $errfile = __FILE__, $line = __LINE__)
    {

        $rootPath = APP_LOG_PATH_NEW . '/' . $dir;
        if (!is_dir($rootPath)) {
            @mkdir($rootPath, 0777, true);
        }
        $file = $rootPath . '/' . date("Y-m-d_H");
        if (is_array($data)) {
            $data['file'] = $errfile;
            $data['line'] = $line;
            $data['user_ip'] =  getClientIp();
            $data['log_time'] = date("Y-m-d H:i:s");
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        file_put_contents($file,$data.PHP_EOL,FILE_APPEND);
    }//end function
}//end class
